
import Vue from 'vue'
import FastClick from 'fastclick'
import router from './router/index.js'
import App from './App'
import  {
  AlertPlugin,
  ConfirmPlugin,
  AjaxPlugin,
  ToastPlugin,
  WechatPlugin,
  LoadingPlugin,
  XButton,
  Popup,
  Group,
  Card,
  XInput,
  Actionsheet,
  Flexbox,
  FlexboxItem,
  Picker,
  Tab,
  TabItem,
  XDialog,
  Checker,
  CheckerItem
} from 'vux'
import store from './vuex/user'
import VueScroller from 'vue-scroller'
import qs from 'qs'
import $config from './libs/config'

window.$config = $config
window.qs = qs
Vue.use(AlertPlugin)
Vue.use(VueScroller)
Vue.use(WechatPlugin)
Vue.use(ConfirmPlugin)
Vue.use(AjaxPlugin)
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)
Vue.component('Group', Group)
Vue.component('Picker', Picker)
Vue.component('XDialog', XDialog)
Vue.component('XButton', XButton)
Vue.component('Card', Card)
Vue.component('Popup', Popup)
Vue.component('XInput', XInput)
Vue.component('Actionsheet', Actionsheet)
Vue.component('Flexbox', Flexbox)
Vue.component('FlexboxItem', FlexboxItem)
Vue.component('Tab', Tab)
Vue.component('TabItem', TabItem)
Vue.component('Checker', Checker)
Vue.component('CheckerItem', CheckerItem)
FastClick.attach(document.body)

Vue.config.productionTip = false

//刷新时,防止登录的数据丢失
if (window.localStorage.user) {
  store.commit('setUserInfo', JSON.parse(window.localStorage.user))
}
if (window.sessionStorage.orderType) {
  store.commit('setOrderType', window.sessionStorage.orderType)
}
//检测需要登录的页面，如果未登录，先去登录
router.beforeEach((to, from, next) => {
  Vue.$vux.confirm.hide()
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const { service_id, uid, verify_car } = store.state.userInfo
    if (uid) {
      const serviceArr = [6, 7, 8]
      if(serviceArr.indexOf(service_id) > -1) {
        if(verify_car == 1 && (service_id == 6 || service_id == 7)) {
          next({
            path: '/warning'
          })
        } else {
          next()
        }
      } else if (service_id === '') {
        next({
          path: '/selectService'
        })
      } else {
        next({
          path: '/warning'
        })
      }
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    if (store.state.userInfo.uid && (to.path == '/login' || to.path == '/reg' || to.path == '/codeLogin' || to.path == '/bindUser')){
      next({
        path: '/'
      })
    } else {
      next()
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app-box')

